package com.e3.template.gg;

import java.util.List;

import com.e3.template.tgf.TGFGridPanel;
import com.e3.web.core.client.GridListener;
import com.e3.web.core.client.GwtAsyncCallback;
import com.e3.web.core.model.SuperModel;
import com.e3.web.widget.client.BaseAction;
import com.e3.web.widget.client.MyButton;
import com.e3.web.widget.client.MyMenu;
import com.e3.web.widget.client.MyMenuItem;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.MenuEvent;
import com.extjs.gxt.ui.client.event.MessageBoxEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.menu.Menu;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.rpc.AsyncCallback;

public abstract class MyGGUI<D extends SuperModel,M extends SuperModel> extends LayoutContainer {
	
	private SelectionListener<ButtonEvent> btnSelectListener = new SelectionListener<ButtonEvent>() {
		@Override
		public void componentSelected(ButtonEvent ce) {
			MyButton item = (MyButton)ce.getSource();
			onBtnMnuEvent(item.getAction());
		}
	};
	
	private SelectionListener<MenuEvent> mnuSelectListener = new SelectionListener<MenuEvent>() {
		@Override
		public void componentSelected(MenuEvent ce) {
			MyMenuItem item = (MyMenuItem)ce.getItem();
			BaseAction action = item.getAction();
			MyMenu menu = (MyMenu)item.getParentMenu();
			action.setSource(menu.getMenuOwner());
			onBtnMnuEvent(item.getAction());
		}
	};
	
	private Listener<MessageBoxEvent> msgBoxListener = new Listener<MessageBoxEvent>(){
		public void handleEvent(MessageBoxEvent be) {
			Button btn = be.getButtonClicked();
			onMessageBoxButton(btn);
		}
	};
	
	private TGFGridPanel<D> masterGrid;
	
	private TGFGridPanel<M> slaveGrid;
	
	@Override
	protected void onRender(Element parent, int index) {
		super.onRender(parent, index);
		setLayout(new FitLayout());
		
		ContentPanel contentPanel = new ContentPanel(new BorderLayout());
		initContentPanel(contentPanel);
		
		ToolBar toobar = new ToolBar();
		initToolBar(toobar);
		
		contentPanel.setTopComponent(toobar);
		add(contentPanel);
	}

	protected void initToolBar(ToolBar toobar) {
		
	}

	protected void initContentPanel(ContentPanel contentPanel) {
		
	}

	public TGFGridPanel<D> getMasterGrid() {
		if(masterGrid==null){
			masterGrid = createMasterGrid();
			Menu menu = getMasterGridContextMenu();
			if(menu!=null){
				masterGrid.setContextMenu(menu);
			}
			masterGrid.addGridListener(new GridListener<D>() {

				public void onGridSelectionChanged(D model) {
					onMasterGridSelectionChanged(model);
				}

				public void onGridRowDoubClicked(D model) {
					onMasterGridRowDoubleClicked(model);
				}
				
			});
		}
		return masterGrid;
	}
	
	protected void onMasterGridSelectionChanged(D model){
		if(model == null){
			return;
		}
		executeQuerySlaves(model,getSlaveModelsAsyncCallback(model));
	}

	protected void onMasterGridRowDoubleClicked(D model){
		
	}
	
	public TGFGridPanel<M> getSlaveGrid() {
		if(slaveGrid==null){
			slaveGrid = createSlaveGrid();
			Menu menu = getSlaveGridContextMenu();
			if(menu!=null){
				slaveGrid.setContextMenu(menu);
			}
			slaveGrid.addGridListener(new GridListener<M>() {

				public void onGridSelectionChanged(M model) {
					onSlaveGridSelectionChanged(model);
				}

				public void onGridRowDoubClicked(M model) {
					onSlaveGridRowDoubleClicked(model);
				}
			});
		}
		return slaveGrid;
	}
	
	protected void onSlaveGridSelectionChanged(M model){
		
	}
	
	protected void onSlaveGridRowDoubleClicked(M model){
		
	}

	public SelectionListener<ButtonEvent> getBtnSelectListener() {
		return btnSelectListener;
	}

	public SelectionListener<MenuEvent> getMnuSelectListener() {
		return mnuSelectListener;
	}

	public Listener<MessageBoxEvent> getMsgBoxListener() {
		return msgBoxListener;
	}
	
	protected void onMessageBoxButton(Button btn) {
		
	}

	protected void onBtnMnuEvent(BaseAction action) {
		
	}

	protected Menu getMasterGridContextMenu(){
		return null;
	}
	
	protected Menu getSlaveGridContextMenu(){
		return null;
	}
	
	protected AsyncCallback<List<M>> getSlaveModelsAsyncCallback(final D model) {
		return new GwtAsyncCallback<List<M>>(this) {
			@Override
			public void doSuccess(List<M> result) {
				getSlaveGrid().setGridValues(result);
			}
		};
	}
	
	protected abstract TGFGridPanel<D> createMasterGrid();
	
	protected abstract TGFGridPanel<M> createSlaveGrid();
	
	protected abstract void executeQuerySlaves(D model,AsyncCallback<List<M>> slaveModelsAsyncCallback);
}
